﻿using ArcShapeFile;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Teigha.DatabaseServices;

namespace Com.OpenCode.DwgConvert.Model
{
    /// <summary>
    /// 默认的转换对象
    /// dwg转化成shp文件
    /// </summary>
    public class DefaultDwgToShpConvert : AbstractDwgConvert
    {
        /// <summary>
        /// 输出的点图层
        /// </summary>
        public ShapeFile PointShp { get; set; }

        /// <summary>
        /// 输出的线图层
        /// </summary>
        public ShapeFile PolyLineShp { get; set; }

        /// <summary>
        /// 输出的面图层
        /// </summary>
        public ShapeFile PolygonShp { get; set; }

        /// <summary>
        /// 任务名称
        /// </summary>
        /// <returns></returns>
        protected override string CreateName()
        {
            return $"(Dwg转Shp){base.CreateName()}";
        }

        /// <summary>
        /// 转换前创建输出数据源
        /// </summary>
        protected override void OnBeforeConvert()
        {
            base.OnBeforeConvert();
            string directory = OutputDatasource.Source;
            AddInfoLogItem($"创建点成果文件(point.shp)");
            PointShp = ShapeFileUtils.CreateShpFile(directory, "point.shp", eShapeType.shpPoint);
            ShapeFileUtils.AddField(PointShp, ShpFieldNames.DataType, eFieldType.shpText, 20);
            ShapeFileUtils.AddField(PointShp, ShpFieldNames.Name, eFieldType.shpText, 255);
            AddInfoLogItem($"创建线成果文件(polyline.shp)");
            PolyLineShp = ShapeFileUtils.CreateShpFile(directory, "polyline.shp", eShapeType.shpPolyLine);
            ShapeFileUtils.AddField(PolyLineShp, ShpFieldNames.DataType, eFieldType.shpText, 20);
            AddInfoLogItem($"创建面成果文件(polygon.shp)");
            PolygonShp = ShapeFileUtils.CreateShpFile(directory, "polygon.shp", eShapeType.shpPolygon);
            ShapeFileUtils.AddField(PolygonShp, ShpFieldNames.DataType, eFieldType.shpText, 20);
        }

        /// <summary>
        /// 默认转换后保存元数据
        /// </summary>
        /// <param name="database"></param>
        protected override void OnAfterConvert()
        {
            base.OnAfterConvert();
            PointShp.Close();
            PointShp.Dispose();
            PolyLineShp.Close();
            PolyLineShp.Dispose();
            PolygonShp.Close();
            PolygonShp.Dispose();
        }

        /// <summary>
        /// 转换点数据
        /// </summary>
        /// <param name="dBPoint"></param>
        protected override void OnConvertDBPoint(DBPoint dBPoint)
        {
            base.OnConvertDBPoint(dBPoint);
            ShapeFileUtils.AddDwgPoint(PointShp, dBPoint);
            PointShp.Fields[ShpFieldNames.DataType].Value = dBPoint.GetType().Name;
            PointShp.WriteShape();
        }

        /// <summary>
        /// dwg文本数据
        /// </summary>
        /// <param name="dBText"></param>
        protected override void OnConvertDBText(DBText dBText)
        {
            base.OnConvertDBText(dBText);
            ShapeFileUtils.AddDwgText(PointShp, dBText);
            PointShp.Fields[ShpFieldNames.DataType].Value = dBText.GetType().Name;
            PointShp.Fields[ShpFieldNames.Name].Value = dBText.TextString;
            PointShp.WriteShape();
        }

        /// <summary>
        /// 转换line
        /// </summary>
        /// <param name="line"></param>
        protected override void OnConvertLine(Line line)
        {
            base.OnConvertLine(line);
            ShapeFileUtils.AddDwgLine(PolyLineShp, line);
            PolyLineShp.Fields[ShpFieldNames.DataType].Value = line.GetType().Name;
            PolyLineShp.WriteShape();
        }

        /// <summary>
        /// 转换多段线
        /// </summary>
        /// <param name="polyline"></param>
        protected override void OnConvertPolyline(Polyline polyline)
        {
            base.OnConvertPolyline(polyline);
            ShapeFileUtils.AddDwgPolyLine(PolyLineShp, polyline);
            PolyLineShp.Fields[ShpFieldNames.DataType].Value = polyline.GetType().Name;
            PolyLineShp.WriteShape();
        }

        /// <summary>
        /// 转换二维多段线
        /// </summary>
        /// <param name="polyline2D"></param>
        protected override void OnConvertPolyline2d(Polyline2d polyline2D)
        {
            base.OnConvertPolyline2d(polyline2D);
            ShapeFileUtils.AddDwgPolyline2d(PolyLineShp, polyline2D);
            PolyLineShp.Fields[ShpFieldNames.DataType].Value = polyline2D.GetType().Name;
            PolyLineShp.WriteShape();
        }

        /// <summary>
        /// 转换三维多段线
        /// </summary>
        /// <param name="polyline3D"></param>
        protected override void OnConvertPolyline3d(Polyline3d polyline3D)
        {
            base.OnConvertPolyline3d(polyline3D);
            ShapeFileUtils.AddDwgPolyline3d(PolyLineShp, polyline3D);
            PolyLineShp.Fields[ShpFieldNames.DataType].Value = polyline3D.GetType().Name;
            PolyLineShp.WriteShape();
        }

        /// <summary>
        /// 转换圆形
        /// </summary>
        /// <param name="circle"></param>
        protected override void OnConvertCircle(Circle circle)
        {
            base.OnConvertCircle(circle);
            ShapeFileUtils.AddDwgCircle(PolyLineShp, circle);
            PolyLineShp.Fields[ShpFieldNames.DataType].Value = circle.GetType().Name;
            PolyLineShp.WriteShape();
        }

        /// <summary>
        /// 转换圆弧
        /// </summary>
        /// <param name="arc"></param>
        protected override void OnConvertArc(Arc arc)
        {
            base.OnConvertArc(arc);
            ShapeFileUtils.AddDwgArc(PolyLineShp, arc);
            PolyLineShp.Fields[ShpFieldNames.DataType].Value = arc.GetType().Name;
            PolyLineShp.WriteShape();
        }

        /// <summary>
        /// 其他类型的数据转换
        /// </summary>
        /// <param name="name"></param>
        /// <param name="entity"></param>
        protected override void OnConvertOthers(string name, Entity entity)
        {
            base.OnConvertOthers(name, entity);
        }
    }
}
